package com.atgui.yygh.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.atgui.yygh.mapper.HospitalSetMapper;
import com.atgui.yygh.model.hosp.Hospital;
import com.atgui.yygh.model.hosp.HospitalSet;
import com.atgui.yygh.repository.HospitalRepository;
import com.atgui.yygh.service.HospitalService;
import com.atgui.yygh.vo.hosp.HospitalQueryVo;
import com.atgui.yygh.vo.order.SignInfoVo;
import com.atguigu.cmn.DictFeignClient;
import com.atguigu.yygh.exception.YyghException;
import com.atguigu.yygh.result.ResultCodeEnum;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * zz: wzh
 * time 2022 08 21 17 11
 **/
@Service
public class HospitalServiceImpl extends ServiceImpl<HospitalSetMapper,HospitalSet> implements HospitalService {
    @Autowired
    private HospitalRepository hospitalRepository;

    @Autowired
    private DictFeignClient dictFeignClient;

    @Override
    public void save(Map<String, Object> map) {
        String mapString = JSONObject.toJSONString(map);
        Hospital hospital = JSONObject.parseObject(mapString, Hospital.class);
        String hoscode = hospital.getHoscode();
        Hospital hospitalExits=hospitalRepository.getHospitalByHoscode(hoscode);
        if (hospitalExits!=null){
            hospital.setStatus(hospitalExits.getStatus());
            hospital.setCreateTime(hospitalExits.getCreateTime());
            hospital.setUpdateTime(new Date());
            hospital.setIsDeleted(0);
            hospitalRepository.save(hospital);
        }else {
            hospital.setStatus(0);
            hospital.setCreateTime(new Date());
            hospital.setUpdateTime(new Date());
            hospital.setIsDeleted(0);
            hospitalRepository.save(hospital);
        }
    }

    @Override
    public Hospital getByHoscode(String hascode) {
        Hospital hospitalByHoscode = hospitalRepository.getHospitalByHoscode(hascode);
        return hospitalByHoscode;
    }

    @Override
    public Page<Hospital> selectHospPage(Integer page, Integer limit, HospitalQueryVo hospitalQueryVo) {
            //创建pageable对象
        Pageable pageable= PageRequest.of(page-1,limit);
        //创建条件匹配器
        ExampleMatcher matcher=ExampleMatcher.matching()
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING)
                .withIgnoreCase(true);
        //将hospitalQueryVo转换为hospital
        Hospital hospital=new Hospital();
        BeanUtils.copyProperties(hospitalQueryVo,hospital);

        Example<Hospital> example=Example.of(hospital,matcher);

        Page<Hospital> pages = hospitalRepository.findAll(example, pageable);
        //进行医院等级封装

        pages.getContent().stream().forEach(this::setHospitalHosType);
        return pages;
    }

    @Override
    public void updateStatus(String id, Integer status) {
        Hospital hospital = hospitalRepository.findById(id).get();
        hospital.setStatus(status);
        hospital.setUpdateTime(new Date());
        hospitalRepository.save(hospital);
    }

    @Override
    public Map<String, Object> getHospById(String id) {
        Map<String,Object> map=new HashMap<>();
        Hospital hospital = this.setHospitalHosType(hospitalRepository.findById(id).get());
        //医院的基本信息
        map.put("hospital",hospital);
        //单独处理更直观
        map.put("bookingRule",hospital.getBookingRule());
        hospital.setBookingRule(null);
        return map;
    }

    @Override
    public String getHospName(String hoscode) {
        Hospital hospital = hospitalRepository.getHospitalByHoscode(hoscode);
        if (hospital!=null){
            return hospital.getHosname();
        }
        return null;
    }

    @Override
    public List<Hospital> findByHosName(String hosname) {
        return hospitalRepository.findHospitalByHosnameLike(hosname);
    }

    @Override
    public Map<String, Object> item(String hoscode) {
        Map<String,Object> map=new HashMap<>();
        //医院详情
        Hospital hospital=this.setHospitalHosType(this.getByHoscode(hoscode));
        map.put("hospital",hospital);
        map.put("bookingRule",hospital.getBookingRule());
        hospital.setBookingRule(null);
        return map;
    }

    //签名信息
    @Override
    public SignInfoVo getSignInfoVo(String hoscode) {
        QueryWrapper<HospitalSet> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("hoscode",hoscode);
        HospitalSet hospitalSet = baseMapper.selectOne(queryWrapper);
        if (hospitalSet==null){
            throw new YyghException(ResultCodeEnum.HOSPITAL_OPEN);
        }
        SignInfoVo signInfoVo=new SignInfoVo();
        signInfoVo.setSignKey(hospitalSet.getSignKey());
        signInfoVo.setApiUrl(hospitalSet.getApiUrl());
        return signInfoVo;
    }


    private Hospital setHospitalHosType(Hospital hospital) {
        String hosType = dictFeignClient.getName("Hostype", hospital.getHostype());
        String provinceName = dictFeignClient.getName(hospital.getProvinceCode());
        String cityName = dictFeignClient.getName(hospital.getCityCode());
        String districtName = dictFeignClient.getName(hospital.getDistrictCode());
        hospital.getParam().put("hostypeString",hosType);
        hospital.getParam().put("fullAddress",provinceName+cityName+districtName);
        return hospital;
    }
}
